<?php
/* 
 * ActionRegistrationBuyer.php
 * for pre-registration validation and action on registration
 * PT BitZero Informatika
 */
require_once APPLICATION_PATH.'/misc/additional.php';
class Application_Controllers_Buyer_ActionRegistrationBuyer
{   public $wrongMessage="";
    //Function to do validation  before Registration
    function PreRegistration($form){
        $buyer = new Application_Model_DbTable_Buyers();
        //get the parameter passed by the controllers
        $username=$form->getValue('username');
        $password=$form->getValue('password');
        $repassword=$form->getValue('repassword');
        $email = $form->getValue('email');
        //create validator for validating the input is between 6 and 20 chars
        $validatorChain = new Zend_Validate();
        $validatorChain->addValidator(
                         new Zend_Validate_StringLength(array('min' => 6,
                         'max' => 20)))
                         ->addValidator(new Zend_Validate_Alnum());
        //Validation's logic begin here

        //check username is it between 6 and 20 chars?
        if(!$validatorChain->isValid($username)){
            //action
            $this->wrongMessage="username harus diantara 6 dan 20 chars";
            return false;
        }
        //check password is it at least 6 char
        if(!$validatorChain->isValid($password)){
            //action
            $this->wrongMessage="Password minimal 6 character";
            return false;
        }
        //check if password have same value with repassword
        if($password != $repassword){
         $this->wrongMessage="Password dan re-password harus sama";
         return false;
        }
        //check whether username already exist or not
        if(!$buyer->checkUser($username)){
            $this->wrongMessage="Username sudah pernah digunakan";
            return false;
        }
        //check email is exist or not
        if(!$buyer->checkEmail($email)){
            $this->wrongMessage="Email sudah pernah digunakan";
            return false;

        }
        return true;
    }
    //function to execute registration main task
    function onRegistration($form){
        $additional = new Additional();
        //initialize object for buyer model
        $buyer = new Application_Model_DbTable_Buyers();
        //initialize object for activation model
        $activation = new Application_Model_DbTable_Activation();
        //getting form data
        $username = $form->getValue('username');
        $password = sha1($form->getValue('password'));
        $email = $form->getValue('email');
        $name = $form->getValue('name');
        $gender = $form->getValue('gender');
        $dob = $form->getValue('date_birth');
        $address = $form->getValue('address');
        $homePhone = $form->getValue('home_phone');
        $handphone=$form->getValue('mobile_phone');
        $city=$form->getValue('city');
        $province=$form->getValue('province');
        $zip=$form->getValue('zip');
        //$avatar=$form->getValue('avatar');
        //inserting buyer
        $buyer->addBuyer($username,$password,$email,$name,$gender,
                                     $dob,$address,$homePhone,$handphone,$city,$province,$zip);
        //generate random number for activation code
        $random = rand(2121312,999999999);

        //check whether activation number already exist or not
        while (!$activation->checkActivationNumber($random)){
               $random = rand(2121312,999999999);
         }

         //get buyer_id
         $buyer_id = $buyer->getBuyerId($username);
         //add activation code and buyer_id into activation
         $activation->addActivation($buyer_id, $random);
         $body = "YTH ".$name.",\n".
                 "Terima kasih atas permohonan Anda untuk bergabung dengan kami\n".
                 "Anda telah terdaftar dengan username :".$form->getValue("username")."\n".
                 "Mohon klik link berikut untuk aktivasi \n".
                 "http://".$_SERVER['HTTP_HOST']."/bitz-plaza/activation?code=".$random;
         $subject="Pendaftaran Berhasil";

         $additional->sendEmail($body, array($form->getValue("email")=>$form->getValue("name"))
                 ,$subject);
         }

}
?>
